package com.bayss.core.service.found;

import com.bayss.core.entity.found.ZaihuMessage;
import com.bayss.core.entity.found.ZaihuMessageComments;
import com.bayss.core.entity.found.ZaihuMessageExample;
import com.bayss.core.entity.found.mapper.ZaihuMessageCommentsMapper;
import com.bayss.core.entity.found.mapper.ZaihuMessageLikeMapper;
import com.bayss.core.entity.found.mapper.ZaihuMessageMapper;
import com.bayss.core.enums.MessageStatus;
import com.bayss.core.enums.RedisKey;
import com.bayss.core.util.FileUtil;
import com.bayss.core.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageService {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ZaihuMessageMapper zaihuMessageMapper;
    @Autowired
    ZaihuMessageCommentsMapper zaihuMessageCommentsMapper;
    @Autowired
    ZaihuMessageLikeMapper zaihuMessageLikeMapper;

    /**
     * 分页查询留言列表
     */
    public PageInfo<ZaihuMessage> getMessageListByPage(String titleName, Integer page, Integer pageSize, Integer status) throws Exception {
        ZaihuMessageExample example = new ZaihuMessageExample();
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page, pageSize);
        ZaihuMessageExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(titleName)) {
            criteria.andTitleNameLike(titleName);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        PageInfo<ZaihuMessage> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> zaihuMessageMapper.selectByExample(example));
        for (ZaihuMessage m : result.getList()) {
            m.setContent(FileUtil.jsonDataImgUrl(m.getContent()));
        }
        return result;
    }

    /**
     * 置顶与取消置顶留言
     *
     * @param placeTop
     * @param messageId
     */
    public void updateMessagePlaceTopByMessageId(Integer placeTop, Integer messageId) {
        ZaihuMessage messageEntity = new ZaihuMessage();
        messageEntity.setId(messageId);
        messageEntity.setPlaceTop(placeTop);
        zaihuMessageMapper.updateByPrimaryKeySelective(messageEntity);
    }

    /**
     * 留言审核
     *
     * @param status:1-待审核；2-审核通过；3-审核拒绝
     */
    public void confirmMessage(Integer id, Integer status) {
        ZaihuMessage messageEntity = new ZaihuMessage();
        messageEntity.setId(id);
        messageEntity.setStatus(status);
        zaihuMessageMapper.updateByPrimaryKeySelective(messageEntity);
    }

    /**
     * 评论审核
     *
     * @param status:1-待审核；2-审核通过；3-审核拒绝
     */
    public void confirmComments(Integer id, Integer status) {
        ZaihuMessageComments messageEntity = new ZaihuMessageComments();
        messageEntity.setId(id);
        messageEntity.setStatus(status);
        zaihuMessageCommentsMapper.updateByPrimaryKeySelective(messageEntity);

        if (status.intValue() == MessageStatus.checked.getVal()) {
            //留言数量+1
            ZaihuMessageComments comments = zaihuMessageCommentsMapper.selectByPrimaryKey(id);
            zaihuMessageMapper.updateComentNumCream1(comments.getMessageId());
        }
    }

    /**
     * 显示和隐藏留言
     *
     * @param messageType
     * @param messageId
     * @return
     */
    public ZaihuMessage updateMessageTypeByMessageId(Integer messageType, Integer messageId) {
        if (messageId == null || messageType == null) {
            return null;
        }
        ZaihuMessage messageEntity = new ZaihuMessage();
        messageEntity.setId(messageId);
        messageEntity.setMessageType(messageType);
        zaihuMessageMapper.updateByPrimaryKeySelective(messageEntity);
        return messageEntity;
    }

    /**
     * 获取留言详情
     *
     * @param messageId
     * @return
     */
    public ZaihuMessage getMessageInfoByMessageId(Integer messageId) {
        ZaihuMessage message = zaihuMessageMapper.selectByPrimaryKey(messageId);
        message.setContent(FileUtil.jsonDataImgUrl(message.getContent()));
        return message;
    }

    /**
     * 发现页开关，0-关闭；1-开启
     *
     * @return
     */
    public void updateMessageOnOff(Integer flag) {
        redisUtil.set(RedisKey.FOUND_ON_OFF, String.valueOf(flag));
    }

    /**
     * 获取发现页开关，0-关闭；1-开启
     *
     * @return
     */
    public int getMessageOnOff() {
        Object flag = redisUtil.get(RedisKey.FOUND_ON_OFF);
        if (flag == null) {
            flag = 1;//首次加載默认开启
        }
        return Integer.valueOf(flag + "");
    }

}
